package cn.zjh.org.controller;

import cn.zjh.basic.exception.BusinessException;
import cn.zjh.basic.util.AjaxResult;
import cn.zjh.basic.util.ExcelUtil;
import cn.zjh.basic.util.PageList;
import cn.zjh.org.domain.Shop;
import cn.zjh.org.domain.ShopAuditLog;
import cn.zjh.org.dto.ShopDto;
import cn.zjh.org.query.ShopQuery;
import cn.zjh.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(value = "店铺管理的api",description = "店铺管理的crud")
@RequestMapping("/shop")
@RestController
public class ShopController {
    @Autowired
    private IShopService shopService;

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    public Shop findOne(@PathVariable("id") Long id) {
        return shopService.findOne(id);
    }

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有")
    @GetMapping
    public List<Shop> findAll() {
        return shopService.findAll();

    }

    /**
     * 分页查询
     * @param shopQuery
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery){
        return shopService.queryPage(shopQuery);
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 添加或修改
     * @param shop
     * @return
     */
    @ApiOperation(value = "添加或修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if(shop.getId() == null){//添加
                shopService.save(shop);
            }else{
                shopService.upDate(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 批量删除
     * @param
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
            shopService.patchDelete(ids);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
     * 接口：店铺入驻
    */
    @PostMapping("/settlement")
    @ApiOperation(value = "店铺入驻")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();//true + "操作成功"
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"入驻失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 接口：店铺审核通过
     * @param
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "店铺审核通过")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.pass(log);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"审核失败");
        }
    }
    /**
     * 接口：店铺审核驳回
     * @param
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "店铺审核驳回")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"驳回失败");
        }
    }

    /**
     * 店铺邮件激活
     * @param
     * @return
     */
    @GetMapping("/active/{id}")
    public AjaxResult active(@PathVariable("id") Long id,HttpServletResponse response){
        try {
                shopService.active(id);
                //重定向跳转
                response.sendRedirect("http://172.16.10.186:8081/#/login");
                return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：导出
     * @param response
     */
    @GetMapping("/export")
    @ApiOperation(value = "导出")
    public void export(HttpServletResponse response){
        try {
            List<Shop> lists = shopService.findAll();
            //lists：导出的数据
            //null:标题 - 一般无
            //"店铺信息" - sheet页的名称
            //Shop.class 导出的类型
            //"shop.xlsx - 导出文件名
            //response - 导出时的响应对象
            ExcelUtil.exportExcel(lists,null,"店铺信息",Shop.class,"shop.xlsx",response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 导入数据：前端要上传
     * el-upload中name属性值="file"
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart MultipartFile file){
        //file - 要导入的文件 - 前端上传的文件
        // 0, 标题行数 - 没有
        // 1, 头函数 = 字段名
        // Shop.class = 导入时映射的对象
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        shops.forEach(System.out::println);
    }


    @GetMapping("/echarts")
    public List<ShopDto> getShopState(){
        return shopService.getShopState();
    }

}
